function LockView(jqKnob, jqBlocks, jqLeftButtons, jqRightButtons, jqStale) {
	this.jqKnob = jqKnob;
	this.jqBlocks = jqBlocks;
	this.jqLeftButtons = jqLeftButtons;
	this.jqRightButtons = jqRightButtons;
	this.jqStale = jqStale;
	this.stale = false;
}

LockView.prototype.init = function(controller) {
	this.controller = controller;
	this.initKnobClick();
	this.initButtons();
};

LockView.prototype.initKnobClick = function() {
	var _this = this;
	this.jqKnob.click(function() {
		if (!_this.stale) {
			_this.controller.onOpenLock();
			for ( var i = 0; i < _this.blocksPositions.length; i++) {
				_this.jqKnob.removeClass('block-' + (i + 1) + '-collision');
			}

			for ( var i = 0; i < _this.blocksPositions.length; i++) {
				var blockPosition = _this.blocksPositions[i];
				if (blockPosition == 2 || blockPosition == 1) {
					_this.animateCollision(i + 1);
					break;
				}
			}
		}
	});
};

LockView.prototype.animateCollision = function(blockIndex) {
	var _this = this;
	setTimeout(function() {
		_this.jqKnob.addClass('block-' + (blockIndex) + '-collision');
	}, 100);
};

LockView.prototype.initButtons = function(blocksPositions) {
	var controller = this.controller;
	var _this = this;

	this.jqLeftButtons.each(function(blockIndex) {
		$(this).click(function() {
			if (!_this.stale) {
				controller.onMove(blockIndex, -1);
			}
		});
	});

	this.jqRightButtons.each(function(blockIndex) {
		$(this).click(function() {
			if (!_this.stale) {
				controller.onMove(blockIndex, +1);
			}
		});
	});
};

LockView.prototype.updateButtons = function(buttonsState) {
	function updateButtonState(elem, state) {
		if (state) {
			elem.removeClass('button-disabled');
		} else {
			elem.addClass('button-disabled');
		}
	}

	for ( var i = 0; i < buttonsState.length; i += 2) {
		var buttonIndex = Math.floor(i / 2);
		var leftButton = $(this.jqLeftButtons.get(buttonIndex));
		updateButtonState(leftButton, buttonsState[i]);

		var rightButton = $(this.jqRightButtons.get(buttonIndex));
		updateButtonState(rightButton, buttonsState[i + 1]);
	}
};

LockView.prototype.update = function(blocksPositions) {
	this.blocksPositions = blocksPositions;

	for ( var i = 0; i < blocksPositions.length; i++) {
		var position = blocksPositions[i];
		var left = (position * 20) + '%';
		$(this.jqBlocks.get(i)).animate({
			left : left
		});
	}
};

LockView.prototype.onSolved = function() {
	this.animateWin();
};

LockView.prototype.animateWin = function() {
	var _this = this;
	this.jqKnob.removeClass('win');
	this.unbind();
	setTimeout(function() {
		_this.jqKnob.addClass('win');
		_this.jqStale.addClass('stale-win');
	}, 1);

	setTimeout(function() {
		alert('Solved!');
	}, 2600);
};

LockView.prototype.unbind = function() {
	this.jqKnob.off();
	this.jqLeftButtons.off();
	this.jqRightButtons.off();
};

LockView.prototype.onStale = function() {
	var controller = this.controller;
	this.jqStale.addClass('stale');
	this.stale = true;
	var _this = this;

	setTimeout(function() {
		_this.jqStale.removeClass('stale');
		_this.stale = false;
		controller.reset();
	}, 1000);
};

LockView.prototype.onReset = function(blocksPositions) {
	this.update(blocksPositions);
};